package com.linkhub.oms.admin.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Description: WebConfig
 * @Author: echo
 * @Date: 2024/3/2 20:22
 * @Version: 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 已在配置文件中指定路径、编码格式
     */
    @Resource
    private MessageSource messageSource;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/doc.html", "/swagger-resources", "/v3/api-docs", "/error");

    }


    @Override
    public Validator getValidator() {
        return validator();
    }

    /**
     * 实体类字段校验国际化引入
     */
    @Bean
    public Validator validator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource);
        return validator;
    }
}
